<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<style>
	.form-group{
		margin-bottom: 0;
	}
</style>
<head th:include="include/head">

</head>

<body class="hold-transition sidebar-mini layout-fixed">
<div class="wrapper">


	<div class="content-wrapper">
		<!-- Content Header (Page header) -->
		<section class="content-header">
			<div class="container-fluid">
				<div class="row mb-2">
					<div class="col-sm-6">
						<h1>资料管理</h1>
					</div>
					<div class="col-sm-6">
						<ol class="breadcrumb float-sm-right">
							<li class="breadcrumb-item"><a href="#">首页</a></li>
							<li class="breadcrumb-item active">资料管理</li>
						</ol>
					</div>
				</div>
			</div>
			<!-- /.container-fluid -->
		</section>
		<section class="content">
			<form id="fileupload" th:action="@{/common/upload}" method="post"
				  style="display: none;">
				<input type="hidden"  id="type" />
				<input type="file" name="file" id="file" />
				<input type="hidden" id="webPath" th:value="${webPath}"/>
			</form>
			<form class="form-horizontal" id="form" method="post"
				  enctype="multipart/form-data" th:action="@{/library/rsave}">

				<div class="card card-info">

					<div class="card-header">
						<h3 class="card-title">基本信息</h3>
					</div>

					<input type="hidden" name="dataId" id="dataId" th:value="${bean.dataId}" />
					<input type="hidden" name="dataType" id="dataType" value="1" />
					<div class="card-body">
						<div class="form-group row">
							<label for="dataName" class="col-sm-2 col-form-label">标题</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" name="dataName" required="required"
									   id="dataName" aria-required="true" th:value="${bean.dataName}">
								<label class="help-block m-b-none" for="dataName"
									   id="dataName-error"><i class="far fa-bell"></i>必填项</label>
							</div>
						</div>
						<div class="form-group row">
							<label for="dataDesc" class="col-sm-2 col-form-label">描述</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" name="dataDesc" required="required"
									   id="dataDesc" aria-required="true" th:value="${bean.dataDesc}">
								<label class="help-block m-b-none" for="dataDesc"
									   id="dataDesc-error"><i class="far fa-bell"></i>必填项</label>
							</div>
						</div>
						<div class="form-group row">
							<label for="dataText" class="col-sm-2 col-form-label">详情</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" name="dataText" required="required"
									   id="dataText" aria-required="true" th:value="${bean.dataText}">
								<label class="help-block m-b-none" for="dataText"
									   id="dataText-error"><i class="far fa-bell"></i>必填项</label>
							</div>
						</div>
						<div class="form-group row">
							<label for="isRequired" class="col-sm-2 col-form-label">是否必读</label>
							<div class="col-sm-10">
								<select  class="form-control"  id="isRequired"  name="isRequired"  required>
									<option value="0" th:selected="${bean.isRequired}==0">否</option>
									<option value="1" th:selected="${bean.isRequired}==1">是</option>
								</select>

								<label class="help-block m-b-none" for="isRequired"
									   id="isRequired-error"><i class="far fa-bell"></i>必填项</label>
							</div>
						</div>
						<div class="form-group row">
							<label for="baseId" class="col-sm-2 col-form-label">所属部门</label>
							<div class="col-sm-10">
								<select  class="form-control"   id="baseId" name="baseId"  required>
									<option value="">选择部门</option>
									<option  th:each="model : ${baseList}" th:value="${model.baseId}"
											 th:text="${model.baseName}" th:selected="${model.baseId}==${bean.baseId}" value="0">否</option>
								</select>
								<input   type="hidden"  name="baseName"  id="baseName">
								<label class="help-block m-b-none" for="baseId"
									   id="baseId-error"><i class="far fa-bell"></i>必填项</label>
							</div>
						</div>
						<div class="form-group row">
							<label for="fileType" class="col-sm-2 col-form-label">附件类型</label>
							<div class="col-sm-10">
								<select id="fileType" name="fileType"   class="form-control" required="required" aria-required="true">
									<option  value="1"  th:selected="${bean.fileType}==1">pdf附件</option>
									<option  value="2"  th:selected="${bean.fileType}==2">doc附件</option>
									<option  value="3"  th:selected="${bean.fileType}==3">视频</option>
								</select>
								<label
										class="help-block m-b-none" for="fileType"
										id="departmentId-error"><i class="far fa-bell"></i>必填项</label>
							</div>
						</div>
						<div class="form-group row"   id="attr" >
							<label  class="col-sm-2 col-form-label">附件文件</label>
							<div class="col-sm-10">
								<button type="button" class="btn btn-primary btn-sm"
										id="addFile">上传附件</button>
								<div class="form-group row">
									<br />
									<div class="card-footer bg-white">
										<ul class="mailbox-attachments d-flex align-items-stretch clearfix">
											<li  class="filediv2" >
												<span class="mailbox-attachment-icon"><i class="far fa-file-alt"></i></span>
												<div class="mailbox-attachment-info">
													<a href="javascript:void(0)" class="mailbox-attachment-name" text="${bean.configUrl}"><i class="fas fa-paperclip"></i>[[${bean.filePath}]] </a>
													<span class="mailbox-attachment-size clearfix mt-1"></span>
													<input  class="picUrl" type="hidden"  id="picUrl"    name="filePath"   th:value="${bean.filePath}" />
												</div>
											</li>
										</ul>
									</div>
								</div>
								<label class="help-block m-b-none"  for="configUrl" id="configUrl-error"><i class="far fa-bell"></i>必填项</label>
							</div>
						</div>
						<div class="form-group row">
							<label for="back3" class="col-sm-2 col-form-label">排序</label>
							<div class="col-sm-10">
								<input type="number" class="form-control" name="back3" required="required"
									   id="back3" aria-required="true" th:value="${bean.back3}">
								<label class="help-block m-b-none" for="back3"
									   id="back3-error"><i class="far fa-bell"></i>必填项</label>
							</div>
						</div>
						<div class="form-group row">
							<label for="dataState" class="col-sm-2 col-form-label">状态</label>
							<div class="col-sm-10">
								<input type="checkbox" class="js-switch" id="state3"  th:checked="${bean.dataState}==1"  />
								<input type="hidden" id="dataState" name="dataState" th:value="${bean.dataState}">
							</div>
						</div>
					</div>
				</div>


			</form>
			<!-- /.card-body -->
			<div class="card-footer">
				<button type="button" class="btn btn-info" onclick="validaBef();">保存</button>
				<button type="button" class="btn btn-default float-right" id="back">取消</button>
			</div>
			<!-- /.card-footer -->


	</section>
	</div>
</div>
<!-- ./wrapper -->
<div th:include="include/foot"></div>

<script>
	var issubmit=false;
	function dosubmit(){
		console.log('issubmit--------------------'+issubmit)
		if(issubmit==false){
			issubmit=true;
			return true;
		}else{
			return false;
		}
	}
	$(document).on("click",".del",function() {
		$(this).parent().parent().remove();

	});
	$('#fileType').change(function (){
		let filediv= $(".filediv2:eq(0)");
		// filedivclone.find('img').attr('src',e.url);
		filediv.find('a').html("<i class='fas fa-paperclip'></i>");
		filediv.parent().find('.picUrl').attr('value','');
		// let  type=$(this).val()
		// if (type==1){
		// 	$('#attr').show()
		// }else{
		// 	$('#attr').hide()
		// }
	})
	$(document).on("click",".delfilediv",function() {
		$(this).parent().remove();

	});
	$("#add_data").click(function(){
		layer.open({
			type: 2,
//		        closeBtn:0,
			title: false,
			area: ['50%', '95%'],
			content: 'add_data',
		});
	});
	$("#addFile").click(function(){
		$("#file").click();
	});
	$("#back").click(function() {
		location.href = 'klist';
	});
	$("#baseId").change(function (){
		$("#baseName").val($('#baseId  option:selected').text())
	})
	$("#baseName").val($('#baseId  option:selected').text())
	$("#file").change(function(e){
	 	let   ftype=	$('#fileType').find("option:selected").val();
	 	console.log(ftype)
		let typeName=$(this).val().substr($(this).val().lastIndexOf("."));
		console.log(typeName);
		if (ftype==1){
			if(typeName!='.pdf'||typeName!='.PDF'){
				layer.alert('请上传pdf文件');
				return;
			}
		}else  if (ftype==2){
			if(typeName!='.doc'||typeName!='.DOC'||typeName!='.docx'||typeName!='.DOCX'){
				layer.alert('请上传doc文件');
				return;
			}
		}else {
			if(typeName!='.mp4'||typeName!='.mp4'||typeName!='.avi'){
				layer.alert('请上传视频文件');
				return;
			}
		}

		var webPath=$("#webPath").val();

		if($(this).val()!=''){
			$("#fileupload").ajaxSubmit({
				success:function(e){
					console.log(e);
					if(e.res){
						let filediv= $(".filediv2:eq(0)");
						// filedivclone.find('img').attr('src',e.url);
						filediv.find('a').html("<i class='fas fa-paperclip'></i>"+webPath+e.url);
						filediv.parent().find('.picUrl').attr('value',webPath+e.url);
						$("#file").val("")
					}

				},error:function(e){
					layer.msg(e.responseText, {icon: 5});
				}
			});
		}

	});
	// 	var elem = document.querySelector('.js-switch');
	// 	var switchery = new Switchery(elem, { color: '#1AB394' });
	var elems = Array.prototype.slice.call(document.querySelectorAll('.js-switch'));
	elems.forEach(function(html) {
		var switchery = new Switchery(html, { color: '#1AB394' });
	});

	$.validator.setDefaults({
		ignore: "",
		highlight : function(a) {
			$(a).removeClass("is-valid").addClass("is-invalid");
		},
		success : function(a) {
			$(a).prev().removeClass("is-invalid").addClass("is-valid");
			$(a).html('<i class="fas fa-check"></i>验证成功');
		},
		errorClass : "help-block m-b-none",
// 			validClass : "help-block m-b-none",
		errorPlacement : function(a, b) {
			if (b.is(":radio") || b.is(":checkbox")) {
				a.appendTo(b.parent().parent().parent())
			} else {
				a.appendTo(b.parent())
			}
		},
		submitHandler:function(form){

			form.submit();
		}
	});
	$.validator.addMethod("checkFile",function(value,element){
		let files=$("input[name='fileurls']");
		if(files.length==0){
			return true;
		}
		return false;
	},"");
	$.validator.addMethod("mobile", function(value, element) {
		var myreg=/^[1][3,4,5,6.7,8,9][0-9]{9}$/;
		var length = value.length;
		return this.optional(element) || (length == 11 && myreg.test(value));
	}, "手机号码格式错误!");
	$("#form").validate(
			{
				ignore : "",
				rules : {
					goodsName : "required",
					doctorPhone: {
						required:true,
						mobile:true
					},
					filehidden : {
						required:true
					},
					attrs : {
						required:true
					},

				},
				messages : {
					goodsName : '<i class="far fa-times-circle"></i>请输入商品名称',
					filehidden : '<i class="far fa-times-circle"></i>请上传图片',
					attrs : '<i class="far fa-times-circle"></i>请选择关联专家',
				}
			});
	function validaBef(){


		if($("#state3").prop("checked")){
			$("#dataState").val("1");
		}else{
			$("#dataState").val("0");
		}
		if ($("input[name='eId']").length>0){
			$("#attrs").attr('value',$("input[name='eId']").length);
		}


		if($("#form").validate()){
			if(dosubmit()){
				$("#form").submit();
			}
			setTimeout(function () {
				issubmit=false
			}, 3000);
		}

	}
	$(document).ready(function (){
		$("#hospitalId").change(function (){
			let v=$(this).val();
			let t= $(this).find("option:selected").text();
			if(v>0){
				$("#hospitalName").val(t);
			}
			$.ajax({url:'../doctor/departmentList',data:"hospitalId="+v,success:function (data){
					if(data.res){
						$("#departmentId").empty();
						$("#departmentId").append("<option value='-1'>请选择科室</option>");
						for(let i=0;i<data.list.length;i++){
							console.log(data.list[i])
							$("#departmentId").append("<option value='"+data.list[i].id+"'>"+data.list[i].departmentName+"</option>");
						}

					}
				}})
		});
		$("#departmentId").change(function (){
			let v=$(this).val();
			let t= $(this).find("option:selected").text();
			console.log(v,t)
			if(v>0){
				$("#departmentName").val(t);
			}

		});
		$("#projectId").change(function (){
			let v=$(this).val();
			let t= $(this).find("option:selected").text();
			if(v>0){
				$("#projectName").val(t);
			}
		});
		$("#baseId").change(function (){
			let v=$(this).val();
			let t= $(this).find("option:selected").text();
			console.log(v,t)
			if(v>0){
				$("#baseName").val(t);
				$.ajax({url:'../library/projectList',data:"baseId="+v,success:function (data){
						console.log(data)
						if(data.res){
							$("#projectId").empty();
							$("#projectId").append("<option value='0'>全部门</option>");
							for(let i=0;i<data.list.length;i++){
								console.log(data.list[i])
								$("#projectId").append("<option value='"+data.list[i].projectId+"'>"+data.list[i].projectName+"</option>");
							}

						}
					}})
			}else {
				$("#projectId").empty();
				$("#projectId").append("<option value='0'>全部门</option>");
			}

		});
	});

</script>
</body>
</html>